
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_WAREHOUSE_PURCHASE_LIST,
    GET_WAREHOUSE_PURCHASE_INFO,
    EDIT_WAREHOUSE_PURCHASE,
    EDIT_WAREHOUSE_PURCHASE_REFUND
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios, deleteAxios} from "@utils/api";

// 采购入库单列表
export const getInventoryPurchaseList = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_PURCHASE_LIST).request,
    params,
    callback
})
// 采购入库单详情
export const getInventoryPurchaseInfo = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_PURCHASE_INFO).request,
    params,
    callback
})
// 创建采购入库单
export const creatInventoryPurchase = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_PURCHASE).request,
    params,
    callback
})
// 创建采购退货单
export const creatInventoryPurchaseRefund = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_PURCHASE_REFUND).request,
    params,
    callback
})

const sagas = {
    * [apiType(GET_WAREHOUSE_PURCHASE_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/purchase/list', params)
            yield put({ type: apiType(GET_WAREHOUSE_PURCHASE_LIST).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_PURCHASE_LIST).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_PURCHASE_INFO).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/purchase/info', params)
            yield put({ type: apiType(GET_WAREHOUSE_PURCHASE_INFO).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_PURCHASE_INFO).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_PURCHASE).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/purchase/info', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_PURCHASE).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_PURCHASE).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_PURCHASE_REFUND).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/purchase/refund', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_PURCHASE_REFUND).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_PURCHASE_REFUND).failure});
        }
    },

}
export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})